package com.frame.pageform.job;

import java.util.*;

/**
 *线程池
 * @author Administrator
 *
 */
public class ThreadPool implements Pool{
	/**
	 * 最大值,-1表示无上限
	 */
	private int max=-1;

	/**
	 * 线程数
	 */
	private List threads = new ArrayList();

	public ThreadPool(){

	}
	public ThreadPool(int poolnum){
		this.max=poolnum;
	}
	private PoolThread getThread() throws Exception{
		PoolThread poolThread=null;
		for(int i=0;i<threads.size();i++){
			PoolThread pt=(PoolThread)threads.get(i);
			if(pt.getThread_state()!=1){
				poolThread=pt;
				break;
			}
		}
		if(poolThread==null){
			if(threads.size()<max || max==-1){
				poolThread=new PoolThread();
				threads.add(poolThread);
			}
		}
		if(poolThread==null)
			throw new Exception("线程池已满,没有空闲的线程接收任务..");
		return poolThread;
	}
	public void execute(Job job) throws Exception {
		// TODO Auto-generated method stub
		getThread().run(job);
	}
}
